#/bin/bash
PATH=/bin:/sbin:/usr/bin/:/usr/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin
# ---------------------------------------------------------------------------------
# 字符云监控(xrkmonitor) 开源版 (c) 2020 by rockdeng
# 使用授权协议： apache license 2.0
#
# 开源演示版网址: http://open.xrkmonitor.com
# 云版本主页：http://xrkmonitor.com
#
# 云版本为开源版提供永久免费告警通道支持，告警通道支持短信、邮件、
# 微信等多种方式，欢迎使用
# ---------------------------------------------------------------------------------
#
# web 前端插件: monitor_website 一键卸载脚本
#

# 网站入口文件
index_files=('index.html' 'index.htm' 'index.shtml' 'index.shtm')

# 正式使用时 agent 会设置 install_log_file, 手动调试可以自行设置
function AddInstallLog()
{
    if [ -f "$install_log_file" ]; then
        logt=`date '+%Y-%m-%d %H:%M:%S.%N'`
        echo "[ $logt ] shell - $1 " >> $install_log_file
    else
        echo $1
    fi
}

if [ ! -x ./install_env.sh ]; then
    AddInstallLog "xrk_failed, not find file:install_env.sh !"
    exit 1
fi
. ./install_env.sh

xrk_success=0
xrk_failed=0

# 移除插件 js 文件目录和文件
if [ -d "$APACHE_DOCUMENT_ROOT/xrkmonitor_plugin" ]; then
    rm -fr "$APACHE_DOCUMENT_ROOT/xrkmonitor_plugin" > /dev/null 2>&1
    if [ -d "$APACHE_DOCUMENT_ROOT/xrkmonitor_plugin" ]; then
        AddInstallLog "remove dir: $APACHE_DOCUMENT_ROOT/xrkmonitor_plugin failed !"
        xrk_failed=`expr $xrk_failed + 1`
    else
        AddInstallLog "remove dir: $APACHE_DOCUMENT_ROOT/xrkmonitor_plugin ok"
        xrk_success=`expr $xrk_success + 1`
    fi
else
    AddInstallLog "js file dir: $APACHE_DOCUMENT_ROOT/xrkmonitor_plugin has not exist"
    xrk_success=`expr $xrk_success + 1`
fi

function RemoveIncFile
{
    incFile="apache_monitor_website.conf"
    cat "$1" |grep $incFile > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        sed -i "/$incFile/d" "$1"
        cat "$1" |grep "$incFile" > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            AddInstallLog "[ change ] - remove $incFile from file: $1 ok"
            xrk_success=`expr $xrk_success + 1`
        else
            AddInstallLog "remove $incFile from file: $1 failed !"
            xrk_failed=`expr $xrk_failed + 1`
        fi
    else
        AddInstallLog "already remove |$incFile| from file: $1"
        xrk_success=`expr $xrk_success + 1`
    fi
}
RemoveIncFile "$APH_SERVER_CONFIG_FILE"

function RemovePLuginJsToIndexFile()
{
    indexflist=`ls -1 "$APACHE_DOCUMENT_ROOT"`

    for inx_file in ${index_files[@]}; do
    	indexf=`echo "$indexflist"|grep -e "^${inx_file}$"`
        if [ $? -ne 0 -o ! -f "$APACHE_DOCUMENT_ROOT/$indexf" ]; then
            indexf=''
            continue
        else
            break;
        fi
    done
	
    if [ "$indexf" == '' ]; then
        AddInstallLog "warning - not find apache site index file, document root:$APACHE_DOCUMENT_ROOT"
        return;
    fi

    cat "$APACHE_DOCUMENT_ROOT/$indexf" |grep "dmt_xrkmonitor.js" > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        sed -i "/dmt_xrkmonitor.js/d" $APACHE_DOCUMENT_ROOT/$indexf 
        cat "$APACHE_DOCUMENT_ROOT/$indexf" |grep "dmt_xrkmonitor.js" > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            AddInstallLog "[ change ] remove plugin js file from index file:$APACHE_DOCUMENT_ROOT/$indexf ok"
            xrk_success=`expr $xrk_success + 1`
        else
            AddInstallLog "[ change ] remove plugin js file from index file:$APACHE_DOCUMENT_ROOT/$indexf failed!"
            xrk_failed=`expr $xrk_failed + 1`
        fi
    else
        xrk_success=`expr $xrk_success + 1` 
        AddInstallLog "not find plugin js file in index file:$APACHE_DOCUMENT_ROOT/$indexf"
    fi
}
RemovePLuginJsToIndexFile

# 虚拟主机处理, 引入插件配置文件, 入口文件引入插件 js 
sAphConfList=`$APACHE_CMD -t -D DUMP_INCLUDES|awk '{if(NR!=1)print $2}'|sort -u`
if [ "$sAphConfList" == '' ]; then
    sAphConfList=`find "$APH_SERVER_CONFIG_PATH" -name "*.conf"`
fi
sVHostFileList=`grep -v "[[:space:]]*#" $sAphConfList|grep "<VirtualHost" |awk -F ":" '{ print $1}'|sort -u`
for sVF in $sVHostFileList
do
    RemoveIncFile "$sVF"
    sDocRootList=`grep -v "[[:space:]]*#" "$sVF"| grep DocumentRoot|awk '{print $2}'`
    for sDoc in $sDocRootList
    do
        sDocRoot=`echo "$sDoc"`
        APACHE_DOCUMENT_ROOT=${sDocRoot//\"/}
        RemovePLuginJsToIndexFile
    done
done

# 这个文件的移除要放后面，否则 apachectl 会报错，因为有其它配置文件引用它
if [ -f "$APACHE_CFG_FILE" ]; then
    rm -f "$APACHE_CFG_FILE" > /dev/null 2>&1
    if [ -f "$APACHE_CFG_FILE" ]; then
        AddInstallLog "remove apache config file:$APACHE_CFG_FILE failed"
        xrk_failed=`expr $xrk_failed + 1`
    else
        AddInstallLog "[ change ] - remove apache config file:$APACHE_CFG_FILE ok"
        xrk_success=`expr $xrk_success + 1`
    fi
else
    AddInstallLog "apache config file:$APACHE_CFG_FILE not exist"
    xrk_success=`expr $xrk_success + 1`
fi


if [ $xrk_failed -eq 0 ]; then 
    echo "xrk_success $xrk_success $xrk_failed";
elif [ $xrk_success -gt 0 ]; then
    echo "xrk_psuccess $xrk_success $xrk_failed";
else
    echo "xrk_failed $xrk_success $xrk_failed";
fi

